if (files.cwd().endsWith("脚本")) {
    let _require = require;
    require = function (path) {
      if (path.startsWith("./")) {
        path = path.replace("./", "");
      }
      path = files.cwd() + "/gdt_hb/" + path;
      return _require(path);
    }
  }
  
  auto.waitFor();
  var utils = require("./utils.js");
  
  var storage = storages.create("xiangxiangyuedu");
  
  function launchAppEnhance(app_name) {
    let thread = threads.start(function () {
      toastLog("检查是否有打开权限!")
      while (true) {
        var allow = text("允许").findOne(5000)
        if (allow) {
          sleep(1000)
          toastLog("发现允许授权!")
          allow.click()
          sleep(1000)
          thread.interrupt()
          break
        }
      }
    })
    sleep(1000)
    var result = launchApp(app_name)
    sleep(3000)
    thread.interrupt()
    return result
  }

function task(){
    
    var 跳过广告 = text("跳过").findOne(10000);
    if(跳过广告){
    跳过广告.click();
    }
    utils.updateAppProgress(10)
    var 我的矿机 = text("我的矿机").findOne(30000);
    if(我的矿机){
    我的矿机.click();
    sleep(3000)
    var 必做任务 = text("必做任务").findOne(3000);
    if(必做任务){
        for(var idx = 1; idx< 6 ;idx++){
        var 观看视频广告任务1 = 必做任务.parent().parent().find(text("去完成"));
        if(观看视频广告任务1){
            var finalActivity = currentActivity()
            观看视频广告任务1.click();
            toastLog("必做任务-观看广告 " + idx + " 个" );
            utils.waitAdvert(finalActivity)
            utils.updateAppProgress(idx * 10)
        }
        }
    }
    var 增值任务 = textContains("增值任务").findOne(3000);
    if(增值任务){
        for(var idx = 1; idx< 9 ;idx++){
        var 观看视频广告任务2 = 增值任务.parent().parent().find(text("去完成"));
        if(观看视频广告任务2){
            var finalActivity = currentActivity()
            观看视频广告任务2.click();
            toastLog("增值任务-观看广告 " + idx + " 个" );
            utils.waitAdvert(finalActivity)
            // utils.updateAppProgress(50 + (idx * 5))
            }
        }
    }

  }
}

function main() {
    let open_app = launchAppEnhance("GES全球节能");
    if (open_app) {
      task()
    }
    else {
      toastLog("GES全球节能打开失败，请确认是否安装并授权打开!");
    }
  }
  
  try {
    main();
    utils.updateAppProgress(100);
  } catch (error) {
    toastLog(error);
  }
  
  storage.put("script_running_status", "end");
  